package org.wangchaocheng.cloudgallery.controller.user;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.SingleResponse;

import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wangchaocheng.cloudgallery.api.user.RoleServiceI;
import org.wangchaocheng.cloudgallery.assembler.ResponseAssembler;
import org.wangchaocheng.cloudgallery.commond.user.RoleSaveCmd;
import org.wangchaocheng.cloudgallery.commond.validate.ValidateCreateGroup;
import org.wangchaocheng.cloudgallery.commond.validate.ValidateUpdateGroup;
import org.wangchaocheng.cloudgallery.dto.common.PageDTO;
import org.wangchaocheng.cloudgallery.dto.user.RoleDTO;
import org.wangchaocheng.cloudgallery.query.user.RolePageQry;

/**
 * 角色管理
 *
 * @author wangchaocheng
 * @date 2025/04/07
 */
@RestController
@RequestMapping("role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleServiceI roleService;

    /**
     * 创建角色
     * @param cmd 参数
     * @return {@link SingleResponse}<{@link Boolean}> 创建结果
     */
    @PostMapping("create")
    public SingleResponse<Boolean> create(@Validated({ValidateCreateGroup.class}) @RequestBody RoleSaveCmd cmd) {
        roleService.save(cmd);
        return SingleResponse.of(true);
    }

    /**
     * 修改角色信息
     *
     * @param cmd 修改参数
     * @return {@link SingleResponse}<{@link Boolean}> 修改结果
     */
    @PutMapping("modify")
    public SingleResponse<Boolean> modify(@Validated({ValidateUpdateGroup.class}) @RequestBody RoleSaveCmd cmd) {
        roleService.save(cmd);
        return SingleResponse.of(true);
    }

    /**
     * 根据角色id查询角色信息
     *
     * @param roleId 角色id
     * @return {@link SingleResponse}<{@link RoleDTO}> 角色信息
     */
    @GetMapping("findById/{roleId}")
    public SingleResponse<RoleDTO> findById(@PathVariable("roleId") Long roleId) {
        RoleDTO roleDTO = roleService.findById(roleId);
        return SingleResponse.of(roleDTO);
    }

    /**
     * 分页条件查询角色列表
     *
     * @param qry 查询条件
     * @return {@link PageResponse}<{@link RoleDTO}> 角色信息
     */
    @GetMapping("listPage")
    public PageResponse<RoleDTO> listPageUser(@RequestBody RolePageQry qry) {
        PageDTO<RoleDTO> rolePage = roleService.listPageRole(qry);
        return ResponseAssembler.INSTANCE.toPageResponse(qry, rolePage);
    }

}
